gravity与layout_gravity的区别

gravity与layout_gravity的区别gravity与layout_gravity的区别

大家好,又见面了,我是你们的朋友全栈君。

含义

gravity是相对于元素本身而言的,用于控制布局中控件的对齐方式,如果没有子控件,则表示内容的对齐方式。gravity常用值有top、bottom、left、right、center等,可以使用”|”将多个值连接起来。

layout_gravity是相对于父布局容器而言的,用于设置在父容器中的对齐方式。

例子

在LinearLayout中放置一个TextView,通过配置gravity和layout_gravity观察TextView出现的位置。
xml文件代码:在TextView中添加属性,android:layout_gravity=”center”

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_bright"
        android:layout_gravity="center"//设置TextView在父控件LinearLayout中居中
        android:text="@string/hello_world" />

</LinearLayout>
在LinearLayout中修改代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:gravity="center">//设置子控件TextView居中
    <TextView
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_bright"
        android:layout_gravity="center"
        android:text="@string/hello_world" />

</LinearLayout>

gravity与layout_gravity的区别=
gravity与layout_gravity的区别


从上面的运行结果就可以看出,android:layout_gravity=”center”是让TextView显示在Layout的中间。
android:gravity=”center”则让LinearLayout中的子控件TextView居中。
再看一个例子

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_bright"
        android:gravity="center_horizontal"//添加代码的位置
        android:layout_gravity="center
        android:text="@string/hello_world" />

</LinearLayout>

gravity与layout_gravity的区别


TextView中没有子控件,那么它会对TextView中的内容起作用。字体在TextView中居中显示。


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/159825.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • pytest的assert_assert断言语句

    pytest的assert_assert断言语句前言断言是写自动化测试基本最重要的一步,一个用例没有断言,就失去了自动化测试的意义了。什么是断言呢?简单来讲就是实际结果和期望结果去对比,符合预期那就测试pass,不符合预期那就测试failed

    2022年7月28日
    3
  • pycharm 查看函数帮助_python中函数和方法区别

    pycharm 查看函数帮助_python中函数和方法区别按住ctrl键,将鼠标放到函数上,就会显示函数信息,点击进去可以查看函数源码。

    2022年8月28日
    2
  • mapminmax 用法

    mapminmax 用法mapminmax是MATLAB实现归一化的工具包,默认:(1)将矩阵的每行分别进行归一化;(2)每行的最大值最小值作为每行归一化的xmin和xmax;(3)将数据归一化到[-1,1].若要将数据归一化到0到1之间,即y∈[0,1],使用b=mapminmax(a,0,1);若给与确定的最大值和最小值作为每行的xmin和xmax,使用:b= mapminmax(a,0,1);PS.xmin…

    2022年6月30日
    19
  • Pycharm 2021.7 EAP的激活码[免费获取]

    (Pycharm 2021.7 EAP的激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月21日
    110
  • idea 2022激活码 csdn【2022最新】

    (idea 2022激活码 csdn)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~FZP9…

    2022年4月1日
    69
  • Java设计模式之行为型:中介者模式

    Java设计模式之行为型:中介者模式

    2021年10月5日
    47

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号